class HashItem {
    public:
    int key;
    int value;
    HashItem *next;
    HashItem(int k, int val) {
        key = k;
        value = val;
        next = NULL;
    }
    ~HashItem() {
        if (next != NULL) delete next;
    }
};

class HashHead {
    public:
    HashItem *next;
    HashHead() {next = NULL;}
    ~HashHead() {
        if (next != NULL) delete next;
    }
    void insert(int key, int value) {
        if (next == NULL) {
            next = new HashItem(key, value);
        }
        else {
            HashItem *tmp = next;
            while (tmp->next != NULL) {
                tmp = tmp->next;
            }
            tmp->next = new HashItem(key, value);
        }
    }
    void remove(int key, int value) {
        if (next == NULL) { return; }
        else if (next->key == key && next->value == value) {
            next = next->next;
            delete next;
        } else {
            HashItem *tmp = next;
            while (tmp->next != NULL) {
                if (tmp->next->key == key && tmp->next->value == value) {
                    tmp->next = tmp->next->next;
                    delete tmp->next;
                    return;
                }
            }
        }
    }
};

class HashSet {
    public:
    int size;
    HashHead *head;
    HashSet(int s) {
        size = s;
        head = new HashHead[s];
    }
    ~HashSet() {
        if (head != NULL) delete []head;
    }
    int Hash_func(int key) {
        return key%size;
    }
    void insert(int key, int value) {
        if (head == NULL) return;
        printf("insert (key=%d,value=%d)\n", key, value);
        head[key%size].insert(key, value);
    }
    void remove(int key, int value) {
        if (head == NULL) return;
        printf("remove (key=%d,value=%d)\n", key, value);
        head[key%size].remove(key, value);
    }
    int search(int key) {
        printf("search for key : %d\n", key);
        if (head == NULL) return -1;
        int index = key%size;
        HashItem *item = head->next;
        while (item != NULL) {
            if (item->key == key) return item->value;
            else item = item->next;
        }
        return -1;
    }
};
